/* 
 * File:   main.cpp
 * Author: jose
 *
 * Created on March 30, 2012, 6:11 PM
 */

#include <cstdlib>
#include <SDL/SDL.h>
#include <SDL/SDL_ttf.h>
#include <iostream>

#include "MainWindow.h"
#include "GameWindow.h"
#include "GameState.h"
#include "GameStatus.h"
#include "EndGame.h"

using namespace std;


/* Game constants */
extern const int SCREEN_HEIGHT = 480;
extern const int SCREEN_WIDTH = 640;
extern const int SCREEN_BPP = 32;

/* Game Enumerators */


/* Game variables */
SDL_Surface *mainScreen = NULL;
GameState *gameState = NULL;
GameStatus *currentGame = NULL;

/* Game Functions*/
bool initGraphics();
void cleanUp();
void changeState(GameStatus::gameStates nextState);
/*
 * 
 */
int main(int argc, char** argv) {
    
    gameState = new MainWindow;
    SDL_Event event;
    
    if(!initGraphics())
    {
        cout << "Could not load the graphics!" << endl;
        return 1;
    }
    
    //New Game
    currentGame = new GameStatus;
    
    while(currentGame->currState != GameStatus::STATE_QUIT)
    {
        while(SDL_PollEvent( &event ))
        {
            if( event.type == SDL_QUIT )
            {
                //Quit the program
                currentGame->setNextState(GameStatus::STATE_QUIT);
            }
        }
        gameState->handleEvents(&event);
        gameState->processLogic(currentGame);
        changeState( currentGame->getNewState() );
        gameState->render(mainScreen);
        
        //Update the screen
        if (SDL_Flip(mainScreen) == -1) {
            return 1;
        }
    }
    
    cleanUp();
    
    return 0;
}


bool initGraphics(){
    //Initialize all SDL subsystems
    if (SDL_Init(SDL_INIT_EVERYTHING) == -1) {
        return false;
    }

    //Initialize SDL_ttf
    if( TTF_Init() == -1 )
    {
        return false;
    }
    
    //Set up the screen
    mainScreen = SDL_SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SDL_SWSURFACE);

    //If there was an error in setting up the screen
    if ( mainScreen == NULL) {
        return false;
    }

    //Set the window caption
    SDL_WM_SetCaption("**** TIC TAC TOE *****", NULL);

    //If everything initialized fine
    return true;
} 

void cleanUp(){
    
    delete mainScreen;
    //Quit SDL_ttf
    TTF_Quit();

    //Quit SDL
    SDL_Quit();
}


void changeState(GameStatus::gameStates nextState) {
        //If the state needs to be changed
    if( nextState != GameStatus::STATE_NULL )
    {
        //Delete the current state
        if( nextState != GameStatus::STATE_QUIT )
        {
            delete gameState;
        }
        
        //Change the state
        switch( nextState )
        {
            case GameStatus::STATE_INTRO:
                gameState = new MainWindow;
                break;

            case GameStatus::STATE_PLAYING:
                gameState = new GameWindow;
                break;

            case GameStatus::STATE_END_GAME:
                gameState = new EndGame();
                break;
        }
    }
}